home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / mail / mh / contrib / queuemh / queuemh-scripts / pland / pland.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  2.7 KB  |  126 lines

  1.  
  2. /*    PLAND SOURCE                        */
  3.  
  4. /*    Pland is a daemon set up to watch a particular fifo    */
  5. /*    which it creates, for an access attempt. When a read    */
  6. /*    attempt is made, It execs a command or script given    */
  7. /*    as an argument at runtime. We are using it here to     */
  8. /*    generate a trouble queue listing for people that    */
  9. /*    "finger touble@cs.colorado.edu"                */
  10.  
  11. /*     
  12. /*    THIS IS THE UNPUBLISHED SOURCE CODE OF REMBO        */
  13. /*    The copyright notice above does not evidence any       */
  14. /*    actual or intended publication of such source code.    */
  15. /*    So, use it if you like, but give me credit.        */
  16.  
  17.  
  18. /*     Usage: plan program_name             */
  19.  
  20.  
  21. /*    Description:                    */
  22.  
  23. /*     This program takes the full pathname of an    */
  24. /*     executable and runs it on a fifo in the     */
  25. /*    user's home directory named .plan.  This    */
  26. /*     way, when finger is executed, the output    */
  27. /*     of the program goes to the fifo.        */
  28.     
  29. /*    Written by:  Tony Rems                 */
  30.  
  31. /*     Updated 9/17/91: Josh Weinberg            */
  32. /*        Added the plan_path argument         */
  33.  
  34. /*     Send bugs and flames to /dev/null or         */
  35. /*    
  36. /*    trouble@boulder.colorado.edu            */
  37.  
  38.  
  39. #include <sys/types.h>
  40. #include <sys/file.h>
  41. #include <stdio.h>
  42. #include <fcntl.h>
  43. #include <sys/stat.h>
  44. #include <signal.h>
  45.  
  46. /* Defines */
  47. #define PERMS 0666
  48. #define USAGE "%s plan_path program_name\n"
  49. /* #define FILENAME "/hazelrah/users/weinberj/.plan" */
  50.  
  51. /* Function prototypes */
  52. void sig_handler();
  53.  
  54. /* Global FILENAME because signal wants a function with no params */
  55.  
  56. char * FILENAME; 
  57.  
  58. main (argc, argv)
  59. int argc;
  60. char *argv[];
  61. {
  62.     int fd;
  63.     int pid;
  64.     int status;
  65.  
  66.     if ( argc !=3  ) {
  67.         fprintf (stderr, USAGE, argv[0]);
  68.         exit(1);
  69.     }  /* if */
  70.     FILENAME = argv[1];
  71.  
  72. /* Catch interrupts for cleanup */
  73.     signal(SIGTERM, sig_handler);
  74.     signal(SIGINT, sig_handler);
  75.     signal(SIGHUP, sig_handler);
  76.  
  77.     unlink (FILENAME);
  78.  
  79. /* Make the fifo */
  80.     if ((mknod(FILENAME, S_IFIFO | PERMS, 0)) < 0 ) {
  81.         perror("mknod");
  82.         exit(2);
  83.     }  /* if */
  84.  
  85.     while (1) {
  86.         if ((fd = open(FILENAME, O_WRONLY)) < 0 ) {
  87.             perror("open");
  88.             exit(3);
  89.         } /* if */
  90.  
  91. /* Once our open completes we know that someone else has
  92.  * opened the FIFO for reading, so we can know run our 
  93.  * program on it.  So, we fork, exec our program and
  94.  * wait for the child to complete.
  95.  */
  96.         switch (pid = fork()) {
  97.             case -1:
  98.                 perror("fork");
  99.                 exit(4);
  100.                 break;
  101.             case 0:
  102. /* If we're in the child, we copy our fifo to stdout */
  103. /* and exec the program given */
  104.                 dup2(fd, 1);
  105.                 execlp(argv[2],argv[2],(void *)NULL);
  106.                 perror("child returned");
  107.                 exit(5);
  108.                 break;
  109.             default:
  110. /* If we're in the parent, we close the pipe and wait */
  111.                 close(fd);
  112.                 while (wait(&status) != pid)
  113.                     ;
  114.                 break;
  115.         } /* switch */
  116.         sleep(2);
  117.         close(fd);
  118.     } /* while */
  119. } /* main */
  120.  
  121. void sig_handler()  /* cleanup */
  122. {
  123.     unlink(FILENAME);
  124.     exit(0);
  125. }
  126.